#include "UIRectangle.h"

CUIRectangle::CUIRectangle(void)
{
	m_pObject = new CUIStructRectangle;
}

CUIRectangle::~CUIRectangle(void)
{
	if( NULL != m_pObject)
	{
		delete m_pObject;
	}

}
void CUIRectangle::FillBack()
{
	CUIStructRectangle * pObj = (CUIStructRectangle *)m_pObject;
	if(pObj == NULL) return;
	CUIWinManager * pFrame = GetFrame();
	if(pFrame ==  NULL) return;
	Graphics * pGraphics = pFrame->m_pGraphics;
	if(pGraphics == NULL) return;
	CUIRect UIRc = GetObjectRect();
	BYTE A = pObj->byAlpha;   
	BYTE r, g, b ;
	r = pObj->crLineColor  & 0xff;
	g = pObj->crLineColor >> 8 & 0xff;
	b = pObj->crLineColor >> 16 & 0xff;

	Color mColor(A,r,g,b);
	LineCap StartCap =(LineCap)pObj->nStartCap;
	LineCap EndCap =(LineCap)pObj->nEndCap; 	
	Pen mPen(mColor, (float)pObj->fLineWidth);
	mPen.SetStartCap(LineCapRound);
	mPen.SetEndCap(LineCapRound);
	mPen.SetAlignment(PenAlignmentInset);

	mPen.SetDashStyle((DashStyle)pObj->iLinetype);  
	mPen.SetLineJoin((LineJoin)pObj->iLineJoin);

	GraphicsPath LayerPath;
	if(pObj->fPercentageRounded > 0 && pObj->fPercentageRounded < 100 )
	{
		CUICommon::MakeRoundRect(&LayerPath,pObj->fPercentageRounded,UIRc);
	}else
	{
		LayerPath.AddRectangle(Rect(UIRc.X,UIRc.Y,UIRc.Width,UIRc.Height));
	}

	Matrix matrix;	
	PointF center((REAL)UIRc.X + UIRc.Width / 2,(REAL)UIRc.Y + UIRc.Height / 2);	
	matrix.RotateAt(pObj->fAngle, center);
	LayerPath.Transform(&matrix);


	switch(pObj->nFillType)
	{
	case FT_SINGLE:
		{
			BYTE r, g, b ;
			r = pObj->crFillColor1 & 0xff;
			g = pObj->crFillColor1 >> 8 & 0xff;
			b = pObj->crFillColor1 >> 16 & 0xff;
			Color mFColor(A,r,g,b);
			SolidBrush mSolidBrush(mFColor);
			pGraphics->FillPath(&mSolidBrush,&LayerPath);
		}
		break; 
	case FT_GRADIENT:
		{
			Gradient(pObj,&LayerPath,pGraphics);
		}
		break; 
	case FT_HATCH:
		{
			BYTE r, g, b ;
			r = pObj->crFillColor1 & 0xff;
			g = pObj->crFillColor1 >> 8 & 0xff;
			b = pObj->crFillColor1 >> 16 & 0xff;
			Color mColor1(A,r,g,b);
			r = pObj->crFillColor2 & 0xff;
			g = pObj->crFillColor2 >> 8 & 0xff;
			b = pObj->crFillColor2 >> 16 & 0xff;
			Color mColor2(A,r,g,b);

			HatchStyle style = (HatchStyle)pObj->nHatchStyle;
			HatchBrush brush_tmp(style, mColor1, mColor2);
			pGraphics->FillPath(&brush_tmp, &LayerPath);
		}
		break;
	case FT_IMAGE:
		{
			Image  image(pObj->sPicPath.c_str(),false);
			pGraphics->DrawImage(&image,UIRc.X,UIRc.Y,UIRc.Width,UIRc.Height);
		}
		break;
	default:
		break;
	}
}
void CUIRectangle::Gradient(CUIStructRectangle * pObj,GraphicsPath * pPath,Graphics * pGraphics)
{
	Rect  UIRc;
	pPath->GetBounds(&UIRc);
	switch(pObj->nGradientMode)
	{
	case GM_ELLIPSE_GRADINET:
		if(!pObj->bEnableMidColor)
		{
			BYTE r, g, b ;
			r = pObj->crFillColor1  & 0xff;
			g = pObj->crFillColor1 >> 8 & 0xff;
			b = pObj->crFillColor1 >> 16 & 0xff;
			Color mColor1(pObj->byAlpha,r,g,b);
			r = pObj->crFillColor2  & 0xff;
			g = pObj->crFillColor2 >> 8 & 0xff;
			b = pObj->crFillColor2 >> 16 & 0xff;
			Color mColor2(pObj->byAlpha,r,g,b);
			SolidBrush mSolidBrush(mColor1);
			pGraphics->SetClip(pPath);

			int iWidth ; 
			double iNewRectWidth; 
			iWidth = (UIRc.Width > UIRc.Height) ? UIRc.Width : UIRc.Height;
			iNewRectWidth = (double) iWidth / 2  / 0.707106781186547524;
			GraphicsPath Ellpsepath;
			PointF center((REAL)UIRc.X + UIRc.Width / 2,(REAL)UIRc.Y + UIRc.Height / 2);
			Ellpsepath.AddEllipse((int)center.X - (int)iNewRectWidth, (int)center.Y - (int)iNewRectWidth,(int)iNewRectWidth * 2,(int)iNewRectWidth * 2);
			PathGradientBrush pthGrBrush(&Ellpsepath);
			pthGrBrush.SetCenterColor(mColor2);
			pthGrBrush.SetFocusScales(0.0f,0.0f);
			int clrCount = 1;
			pthGrBrush.SetSurroundColors(&mColor1, &clrCount);
			pGraphics->FillEllipse(&pthGrBrush, (int)center.X - (int)iNewRectWidth, (int)center.Y - (int)iNewRectWidth,(int)iNewRectWidth * 2,(int)iNewRectWidth * 2);

		}else
		{
			BYTE r, g, b ;
			r = pObj->crFillColor1  & 0xff;
			g = pObj->crFillColor1 >> 8 & 0xff;
			b = pObj->crFillColor1 >> 16 & 0xff;
			Color mColor1(pObj->byAlpha,r,g,b);
			r = pObj->crFillColor2  & 0xff;
			g = pObj->crFillColor2 >> 8 & 0xff;
			b = pObj->crFillColor2 >> 16 & 0xff;
			Color mColor2(pObj->byAlpha,r,g,b);

			Color mColor3(pObj->byAlpha,r,g,b);
			r = pObj->crMidColor  & 0xff;
			g = pObj->crMidColor >> 8 & 0xff;
			b = pObj->crMidColor >> 16 & 0xff;
			pGraphics->SetClip(pPath);

			int iWidth ; 
			double iNewRectWidth; 
			iWidth = (UIRc.Width > UIRc.Height) ? UIRc.Width : UIRc.Height;
			iNewRectWidth = (double) iWidth / 2  / 0.707106781186547524;
			GraphicsPath Ellpsepath;
			PointF center((REAL)UIRc.X + UIRc.Width / 2,(REAL)UIRc.Y + UIRc.Height / 2);
			Ellpsepath.AddEllipse((int)center.X - (int)iNewRectWidth, (int)center.Y - (int)iNewRectWidth,(int)iNewRectWidth * 2,(int)iNewRectWidth * 2);


			PathGradientBrush pthGrBrush(&Ellpsepath);
			pthGrBrush.SetCenterColor(mColor2);
			pthGrBrush.SetFocusScales(0.0f,0.0f);
			int clrCount = 1;
			pthGrBrush.SetSurroundColors(&mColor1, &clrCount);

			Color cols[] = {mColor1, mColor3, mColor2};
			REAL bps[] = {0.0f, 0.5f, 1.0f};
			pthGrBrush.SetInterpolationColors(/*Color**/cols, /*REAL**/bps, /*int*/3);	
			pGraphics->FillEllipse(&pthGrBrush, (int)center.X - (int)iNewRectWidth, (int)center.Y - (int)iNewRectWidth,(int)iNewRectWidth * 2,(int)iNewRectWidth*2);
		}
		break;
	case GM_RECTANGLE_GRADINET:
		{
			if(!pObj->bEnableMidColor)
			{
				BYTE r, g, b ;
				r = pObj->crFillColor1  & 0xff;
				g = pObj->crFillColor1 >> 8 & 0xff;
				b = pObj->crFillColor1 >> 16 & 0xff;
				Color mColor1(pObj->byAlpha,r,g,b);
				r = pObj->crFillColor2  & 0xff;
				g = pObj->crFillColor2 >> 8 & 0xff;
				b = pObj->crFillColor2 >> 16 & 0xff;
				Color mColor2(pObj->byAlpha,r,g,b);


				GraphicsPath RectanglePath;
				RectanglePath.AddPath(pPath,TRUE);
				PathGradientBrush pthGrBrush(&RectanglePath);
				pthGrBrush.SetCenterColor(mColor2);
				pthGrBrush.SetFocusScales(0.0f,0.0f);
				int clrCount=1;
				pthGrBrush.SetSurroundColors(&mColor1, &clrCount);
				pGraphics->FillPath(&pthGrBrush,pPath);
			}else
			{
				BYTE r, g, b ;
				r = pObj->crFillColor1  & 0xff;
				g = pObj->crFillColor1 >> 8 & 0xff;
				b = pObj->crFillColor1 >> 16 & 0xff;
				Color mColor1(pObj->byAlpha,r,g,b);
				r = pObj->crFillColor2  & 0xff;
				g = pObj->crFillColor2 >> 8 & 0xff;
				b = pObj->crFillColor2 >> 16 & 0xff;
				Color mColor2(pObj->byAlpha,r,g,b);

				Color mColor3(pObj->byAlpha,r,g,b);
				r = pObj->crMidColor  & 0xff;
				g = pObj->crMidColor >> 8 & 0xff;
				b = pObj->crMidColor >> 16 & 0xff;

				GraphicsPath RectanglePath;
				RectanglePath.AddPath(pPath,TRUE);
				PathGradientBrush pthGrBrush(&RectanglePath);
				pthGrBrush.SetCenterColor(mColor2);
				pthGrBrush.SetFocusScales(0.0f,0.0f);
				int clrCount = 1;
				pthGrBrush.SetSurroundColors(&mColor1, &clrCount);
				Color cols[] = {mColor1, mColor3, mColor2};
				REAL bps[] = {0.0f, 0.5f, 1.0f};
				pthGrBrush.SetInterpolationColors(/*Color**/cols, /*REAL**/bps, /*int*/3);	
				pGraphics->FillPath(&pthGrBrush,pPath);
			}
		}
		break;
	case GM_LINEAR_GRADINET:
		{
			if(!pObj->bEnableMidColor)
			{
				BYTE r, g, b ;
				r = pObj->crFillColor1  & 0xff;
				g = pObj->crFillColor1 >> 8 & 0xff;
				b = pObj->crFillColor1 >> 16 & 0xff;
				Color mColor1(pObj->byAlpha,r,g,b);
				r = pObj->crFillColor2  & 0xff;
				g = pObj->crFillColor2 >> 8 & 0xff;
				b = pObj->crFillColor2 >> 16 & 0xff;
				Color mColor2(pObj->byAlpha,r,g,b);
				LinearGradientBrush mLineGrBrush(Rect(UIRc.X,UIRc.Y,UIRc.Width,UIRc.Height),mColor1, mColor2,pObj->nGradientAngle + pObj->fAngle);
				pGraphics->FillPath(&mLineGrBrush, pPath);
			}else
			{
				BYTE r, g, b ;
				r = pObj->crFillColor1  & 0xff;
				g = pObj->crFillColor1 >> 8 & 0xff;
				b = pObj->crFillColor1 >> 16 & 0xff;
				Color mColor1(pObj->byAlpha,r,g,b);
				r = pObj->crFillColor2  & 0xff;
				g = pObj->crFillColor2 >> 8 & 0xff;
				b = pObj->crFillColor2 >> 16 & 0xff;
				Color mColor2(pObj->byAlpha,r,g,b);

				Color mColor3(pObj->byAlpha,r,g,b);
				r = pObj->crMidColor  & 0xff;
				g = pObj->crMidColor >> 8 & 0xff;
				b = pObj->crMidColor >> 16 & 0xff;

				LinearGradientBrush mLineGrBrush(Rect(UIRc.X,UIRc.Y,UIRc.Width,UIRc.Height),mColor1, mColor2,pObj->nGradientAngle + pObj->fAngle);
				Color cols[] = {mColor1, mColor3, mColor2};
				REAL bps[] = {0.0f, 0.5f, 1.0f};
				mLineGrBrush.SetInterpolationColors(cols, bps, 3);					
				pGraphics->FillPath(&mLineGrBrush, pPath);
			}
			break;
		}
		break;
	}
}
void CUIRectangle::OnMoseMove(long ptX, long ptY,long KeyFlag)
{

}
void CUIRectangle::OnLButtonUp(long ptX, long ptY)
{

}
void CUIRectangle::OnCommand(long CommandID)
{

}
void CUIRectangle::OnDraw(bool bUpdate)
{
	if(m_pObject != NULL)
	{
		FillBack();
	}
}